<template>
  <div class="mod-sys__dishes">
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <el-form-item>
        <el-input v-model="state.dataForm.name" placeholder="菜品名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="state.getDataList()">{{ $t("query") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="state.exportHandle()">{{ $t("export") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('sys:dishes:save')" type="primary" @click="addOrUpdateHandle()">{{ $t("add") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('sys:dishes:delete')" type="danger" @click="state.deleteHandle()">{{ $t("deleteBatch") }}</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" border @selection-change="state.dataListSelectionChangeHandle" style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="dishId" label="编号" header-align="center" align="center"></el-table-column>
      <el-table-column prop="name" label="菜品名称" header-align="center" align="center"></el-table-column>
      <!-- <el-table-column prop="description" label="菜品描述" header-align="center" align="center"></el-table-column> -->
      <el-table-column prop="price" label="菜品价格" header-align="center" align="center"></el-table-column>
      <el-table-column prop="imageUrl" label="图片大图" header-align="center" align="center">
        <template v-slot="scope">
          <small-image :style="`--my-width: ${50}px; --my-height: ${50}px;`" :url="scope.row.imageUrl" />
        </template>
      </el-table-column>
      <el-table-column prop="thumbnailUrl" label="缩略图" header-align="center" align="center">
        <template v-slot="scope">
          <small-image :style="`--my-width: ${50}px; --my-height: ${50}px;`" :url="scope.row.thumbnailUrl" />
        </template>
      </el-table-column>
      <el-table-column prop="isActive" label="在售状态" header-align="center" align="center">
        <template v-slot="scope">
          <el-tag v-if="scope.row.isActive === 0" size="small" type="danger">禁用</el-tag>
          <el-tag v-else size="small" type="success">启用</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="categoryName" label="分类名称" header-align="center" align="center"></el-table-column>
      <el-table-column width="200px" prop="dishFlavorId" label="标签" header-align="center" align="center">
        <template v-slot="scope">
          <el-tag style="margin: 4px;" v-for="tagName in scope.row.tagNames" size="small" type="success">{{tagName}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column width="200px" prop="dishFlavorId" label="口味" header-align="center" align="center">
        <template v-slot="scope">
          <el-tag style="margin: 4px;" v-for="val in scope.row.flavorValues" size="small" type="success">{{val}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column width="200px" prop="eatType" label="就餐类型" header-align="center" align="center">
        <template v-slot="scope">
          <el-tag style="margin: 4px;" type="success" :key="val" v-for="val in eatTypeValue(scope.row.eatType)">{{val}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createdAt" label="创建时间" header-align="center" align="center"></el-table-column>
      <el-table-column prop="updatedAt" label="更新时间" header-align="center" align="center"></el-table-column>
      <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
        <template v-slot="scope">
          <el-button v-if="state.hasPermission('sys:dishes:update')" type="primary" link @click="addOrUpdateHandle(scope.row.dishId)">{{ $t("update") }}</el-button>
          <el-button v-if="state.hasPermission('sys:dishes:delete')" type="primary" link @click="state.deleteHandle(scope.row.dishId)">{{ $t("delete") }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit" :total="state.total" layout="total, sizes, prev, pager, next, jumper" @size-change="state.pageSizeChangeHandle" @current-change="state.pageCurrentChangeHandle"> </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update :key="addKey" ref="addOrUpdateRef" @refreshDataList="state.getDataList"></add-or-update>
  </div>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import { nextTick, reactive, ref, toRefs, watch } from "vue";
import AddOrUpdate from "./dishes-add-or-update.vue";
import SmallImage from "../../components/small-image/index.vue";
import { useAppStore } from "@/store";
import { getDictDataList } from "@/utils/utils";
const store = useAppStore();
const view = reactive({
  getDataListURL: "/sys/dishes/page",
  getDataListIsPage: true,
  exportURL: "/sys/dishes/export",
  deleteURL: "/sys/dishes",
  deleteIsBatch: true,
  deleteIsBatchKey: "dishId", // 主键id
  dataForm: {
    name: "",
  }
});

const state = reactive({ ...useView(view), ...toRefs(view) });


const addKey = ref(0);
const addOrUpdateRef = ref();
const addOrUpdateHandle = (dishId?: number) => {
  addKey.value++;
  nextTick(() => {
    addOrUpdateRef.value.init(dishId);
  });
};

const eatTypeValue = (eatType: string) => {
  return getDictDataList(store.state.dicts, 'eat_type').filter((item:any) => {
    return eatType.split(",").includes(item.dictValue);
  }).map((item:any) => {
    return item.dictLabel;
  });
}
</script>
